require.config({
    paths: {
        'jquery': 'http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js',
        'vue': '../encaptool/encvue',
        'iview': '../utiltool/iview/iview',
        'bmap': 'http://api.map.baidu.com/api?v=2.0&ak=LYcRZZyiZTRlGLgHcxzBV07VmePXlUR2',
        'async': '../utiltool/requirejsplugins/src/async',
        'axios': '../encaptool/encaxios',
        'heatmap': '../utiltool/heatmap/Heatmap_min',
        'lushu': '../utiltool/LuShu/LuShu_min',
        'echarts': "../utiltool/echarts/echarts.min",
        'router': '../encaptool/encarouter',
    },
    shim: {//依赖关系
        'iview': {
            deps: ['vue']
        },
        'vueEventCalendar': {
            deps: ['vue']
        },
        'bmap': {
            deps: ['jquery'],
            exports: 'BMap'
        }
    }
})

require(['vue', 'iview', "echarts", 'axios','router', 'jquery', 'async!bmap', 'heatmap', 'lushu'], function (Vue, iview, echarts, axios,Router) {
    Vue.use(iview);
    Vue.use(echarts);
    let parent = {
        template:"<div id='button-list'><i-button id='mile_com' type='primary'>里程对比</i-button><i-button id='turns_com' type='primary'>运送次数对比</i-button><i-button id='Average' type='primary'>月均值对比</i-button><i-button id='all_com' type='primary'>总计算占比</i-button></div>"
    }
    let loading = {
        template:"<div style='display:block'>"
                +"  <div id='loading'>"
                +"      <div class='loader-03'>"
                +"      </div>"
                +"      <div class='load-03-bar' id='loading-change'></div>"
                +"  </div>"
                +"</div>"
    }
    new Vue({
        el: "#pathAnalysis",
        data() {
            return {
                cityList: [{
                    name: '原始里程与节约里程分析',
                    value: 0
                },
                {
                    name: '样本原始里程对比',
                    value: 1
                }
                ],
                loadBar:null,
                model1:[0],//默认选择原始距离与聚类
                map: {},
                points: [],
                newInfo: {
                    factoryname: "",
                    longitude: 116.82,
                    latitude: 40.01
                },
                // tableHeight:200,
                newPathDistnceSum: 0,
                oldPathDistnceSum: 0,
                lushu: {},
                starttime:null,
                endtime:null,
                jsonName: '',
                jsonNames: [
                    {
                        value: '-20160103.json',
                        label: '2016-01-03',
                    },
                    {
                        value: '-20160323.json',
                        label: '2016-03-23',
                    },
                ],
                dateoption: {
                    //时间控件日期过滤
                    disabledDate: (date => {
                        let date_return = (date && date.valueOf() < new Date(this.date_init[0]) - 86400000)
                        let date_init = this.date_init
                        for (let i in date_init){
                            let j = parseInt(i)+1
                            let changeDate = parseInt(date_init[i].split("-")[2])+1
                            if(changeDate/10<1){
                                changeDate = "0" + changeDate
                            }
                            let now_date = date_init[i].split("-")[0]+'-'+date_init[i].split("-")[1]+'-'+changeDate
                            date_return += ((date && date.valueOf() > new Date(now_date) - 86400000) && (date && date.valueOf() < new Date(date_init[j])- 86400000))
                        }
                        date_return += (date && date.valueOf() > new Date(this.date_init[date_init.length]) - 86400000)
                    return date_return
                    }).bind(this)
                  },
                isDisabledStart:true,
                isDisabledEnd:true,
                date_init:[],
                pathNewData: {},
                pathOldData: {},
                saveDistance: 0,
                newDistance: 0,
                oldDistance: 0,
                comp_dis : 0,
                comp_turn : 0,
                comp_bourn:0,
                oldtrun:0,
                newtrun:0,
                test_all:0,
                selet_mouth:0,
                pathColumns: [{
                        title: '节约里程配送路径',
                        key: 'pathName',
                        align: 'center',
                        width: 0,
                        minWidth: 0,
                        sortable: true,
                    },
                    {
                        title: '节约里程配送距离/公里',
                        key: 'distance',
                        width: 0,
                        minWidth: 0,
                        sortable: true,
                        align: 'center'
                    }
                ],
                pathColumns_old: [{
                    title: '原始配送路径',
                    key: 'pathName',
                    align: 'center',
                    width: 0,
                    minWidth: 0,
                    sortable: true,
                },
                {
                    title: '原始配送距离/公里',
                    key: 'distance',
                    width: 0,
                    minWidth: 0,
                    sortable:  true,
                    align: 'center'
                }
            ],
                pathTableDataNew: [],
                pathTableDataOld: [],
                overlaysId: 0
            }
        },
        components:{
            parent,
            loading
          },
        watch: {
            oldDistance:'drawChart',
            newDistance:'drawChart',
            model1:"selectComp",
            loadBar:"loadingChange"
        },
        computed:{
            loadingBar:()=>{
                return this.loadBar
            }
        },
        mounted() {
            this.initMap();
            this.DateInit();
        },
        methods: {
            DateInit(){
                let setting = {
                    method: 'post',
                    url: "../static/json/insert_time.txt"
                }
                axios.call(this, function (res) {
                    this.date_init = res.split(",")
                    console.log(this.date_init)
                }.bind(this),{
                    setting: setting
                })
            },
            dataInit(){
                let totalChart2 = echarts.init(document.getElementById('totalChart2'));
                totalChart2.clear();
                this.map.clearOverlays()
            },
            DateArr(date){
                let date_return = (date && date.valueOf() < new Date(this.date_init[0]) - 86400000) + (date && date.valueOf() > new Date('2016-01-06') - 86400000 && date && date.valueOf() < new Date(this.date_init[1]) - 86400000) + (date && date.valueOf() > new Date('2016-01-13') - 86400000 && date && date.valueOf() < new Date(this.date_init[3]) - 86400000)
                    for (let i in this.date_init){
                        let changeDate = parseInt(this.date_init[i].split("-")[2])+1
                        if(changeDate/10<1){
                            changeDate = "0" + changeDate
                        }
                        let now_date = this.date_init[i].split("-")[0]+'-'+this.date_init[i].split("-")[1]+'-'+changeDate
                        date_return += (date && date.valueOf() > new Date(now_date) - 86400000 && date && date.valueOf() < new Date(this.date_init[i+1])- 86400000)
                    }
                return date_return
            },
            selectComp(){
                this.chartOpen();
                if (!this.model1) {
                    console.log(this.model1)
                    this.isDisabledStart = false
                    this.isDisabledEnd = true
                    document.getElementById("Average").innerText="月均值对比"
                }
                else{
                    this.isDisabledStart = false
                    this.isDisabledEnd = false
                    document.getElementById("Average").innerText="数据对比"
                }
            },
            handleChange(date){
                this.starttime = date
                if (!this.model1) {
                    if(date){
                        this.chartOpen();
                        let read_json_file_t = date.split("-").join("");
                        this.getTime(read_json_file_t,read_json_file_t,this.model1);
                    }
                    if(!date){
                        this.dataInit();
                        document.getElementById("path-tables").style.display = "none"
                    }
                } else {
                    if(!date){
                        this.dataInit();
                        document.getElementById("path-tables").style.display = "none"
                    }
                }
                
            },
            endChange(date){
                this.chartOpen();
                this.endtime = date;
                if(date){
                    if(this.starttime){
                        console.log("endinit")
                        let read_json_file_t = this.starttime.split("-").join("");
                        let comp_json_file_t = date.split("-").join("");
                        this.getTime(read_json_file_t,comp_json_file_t,this.model1);
                    }
                }
            },
            chartOpen(){
                this.endtime = null;
                document.getElementById("path-tables").style.display = "block"
                this.dataInit();
            },
            chartWidth(){
                let width =  document.body.clientWidth;
                let all_width = (width-250)*0.37/2
                // let height = document.body.clientHeight;
                // this.tableHeight = height*0.2;
                for(let i in this.pathColumns){
                    this.pathColumns[i].minWidth = all_width
                    this.pathColumns_old[i].minWidth = all_width
                    this.pathColumns[i].width = all_width
                    this.pathColumns_old[i].width = all_width
                }
               
            },
            initMap: function () {
                this.chartWidth();
                this.setMap();
                this.addFactoryPoint();
            },
            getTime(read_json_file_t,comp_json_file_t,type){
                if (!type) {
                    this.getPathJsonOld(read_json_file_t);
                    this.getPathJsonNew(read_json_file_t,type);
                } else {
                    this.getPathJsonOld(read_json_file_t);
                    this.getPathJsonNew(comp_json_file_t,type);
                }

            },
            setMap() {
                let map = new BMap.Map("pathAnalysisMap");
                map.centerAndZoom(new BMap.Point(116.654788, 39.919362), 11);
                map.enableScrollWheelZoom();
                this.map = map;
            },
            //渲染json数据
            initDrv: function (pathData, color, e, dType) {
                let pathTableData = [];
                e = e || -1;
                if(e!=-1){
                    e-=1;
                }
                let _this = this;
                pathData = pathData || [];
                color = color || '#1453e2';
                let drv = new BMap.DrivingRoute('中国', {
                    onSearchComplete: function (res) {
                        if (drv.getStatus() == BMAP_STATUS_SUCCESS) {
                            let plan = res.getPlan(0);
                            let arrPois = []
                            console.log(res)
                            for (let j = 0; j < plan.getNumRoutes(); j++) {
                                let route = plan.getRoute(j);
                                arrPois = arrPois.concat(route.getPath());
                            }
                            _this.map.addOverlay(new BMap.Polyline(arrPois, {
                                strokeColor: color
                            }));
                        }
                    }

                });
                if (e != -1) {
                    pathData = dType == 0 ? this.pathNewData : this.pathOldData;
                    if (this.overlaysId === 0) {
                        this.overlaysId = this.map.getOverlays().length
                    } else {
                        for (let i = this.map.getOverlays().length; i > this.overlaysId; i--) {
                            this.map.removeOverlay(this.map.getOverlays()[i - 1])
                        }
                    }
                    let start = new BMap.Point(116.830302, 40.012665);
                    if (pathData[e.toString()].path.length) {
                        for (let i = 0; i < pathData[e.toString()].path.length; i++) {
                            let end = new BMap.Point(pathData[e.toString()].path[i].coordinate[0], pathData[e.toString()].path[i].coordinate[1]);
                            drv.search(start, end);
                            start = new BMap.Point(pathData[e.toString()].path[i].coordinate[0], pathData[e.toString()].path[i].coordinate[1]);
                        }
                        return;
                    }
                    let end = new BMap.Point(pathData[e.toString()].path.coordinate[0], pathData[e.toString()].path.coordinate[1]);
                    drv.search(start, end);
                    this.map.centerAndZoom(new BMap.Point(pathData[e.toString()].path.coordinate[0], pathData[e.toString()].path.coordinate[1]), 10);
                    return;
                }
                // this.loading();
                for (let i = 0; i < pathData.length; i++) {
                    // this.loadBar = i/pathdataone.path.length;
                    this.loadBar = i;
                    let start = new BMap.Point(116.830302, 40.012665);//总部坐标
                    let end;
                    let pathdataone = pathData[i];
                    let creatjson = {};
                    let pathStr = "";
                    let volumeSum = 0;
                    creatjson.distance = pathdataone.distance/1000;
                    if (pathdataone.path.length) {
                        for (let x = 0; x < pathdataone.path.length; x++) {
                            
                            pathStr == "" ? pathStr = pathdataone.path[x].addressName : pathStr += pathStr + '→' + pathdataone.path[x].addressName;
                            volumeSum == 0 ? volumeSum = pathdataone.path[x].actual_volume : volumeSum = volumeSum + pathdataone.path[x].actual_volume;
                            if (pathdataone.path[x].coordinate[0] > pathdataone.path[x].coordinate[1]) {
                                end = new BMap.Point(pathdataone.path[x].coordinate[0], pathdataone.path[x].coordinate[1]);
                                drv.search(start, end);
                                start = new BMap.Point(pathdataone.path[x].coordinate[0], pathdataone.path[x].coordinate[1]);
                            } else {
                                end = new BMap.Point(pathdataone.path[x].coordinate[1], pathdataone.path[x].coordinate[0]);
                                drv.search(start, end);
                                start = new BMap.Point(pathdataone.path[x].coordinate[1], pathdataone.path[x].coordinate[0]);
                            }
                        }
                        creatjson.pathName = pathStr;
                        creatjson.path_volume = volumeSum/1000;
                        creatjson.id = i;
                        creatjson.dType = dType;
                        pathTableData.push(creatjson);
                        continue;
                    }
                    pathStr = pathdataone.path.addressName;
                    volumeSum = pathdataone.path.actual_volume;
                    creatjson.pathName = pathStr;
                    creatjson.path_volume = volumeSum/1000;
                    creatjson.id = i;
                    creatjson.dType = dType;
                    pathTableData.push(creatjson);
                    pathdataone.path.coordinate[0] > pathdataone.path.coordinate[1] ?
                        (end = new BMap.Point(pathdataone.path.coordinate[0], pathdataone.path.coordinate[1])) :
                        (end = new BMap.Point(pathdataone.path.coordinate[0], pathdataone.path.coordinate[1]));
                    drv.search(start, end);
                    console.log(drv)
                    // this.loadBar += 1;
                }
                
                dType === 0 ? this.pathTableDataNew = pathTableData : this.pathTableDataOld = pathTableData;
                this.map.centerAndZoom(new BMap.Point(116.654788, 39.919362), 10);
            },
            loadingChange: function(){
                document.getElementById('loading-change').innerHTML=this.loadBar
            },
            addFactoryPoint: function () {
                let content = `<p slot="title">总部</p>`;
                let marker = new BMap.Marker((new BMap.Point(116.830302, 40.012665)));
                this.map.addOverlay(marker);
                this.addClickHandler(content, marker);
            },
            addDistribution: function (distributionData) {
                
                this.addFactoryPoint()
                distributionData = distributionData || {};
                let icon = new BMap.Icon("/YF/public/static/img/marker0.png", new BMap.Size(30, 30));
                for (let i = 0; i < distributionData.length; i++) {
                    let distributionDataOne = distributionData[i];
                    let marker;
                    let content;
                    if (distributionDataOne.path.length) {
                        for (let x = 0; x < distributionDataOne.path.length; x++) {
                            distributionDataOne.path[x].coordinate[0] = parseFloat(distributionDataOne.path[x].coordinate[0]) ?
                                distributionDataOne.path[x].coordinate[0] : parseFloat(distributionDataOne.path[x].coordinate[0])
                            distributionDataOne.path[x].coordinate[1] = parseFloat(distributionDataOne.path[x].coordinate[1]) ?
                                distributionDataOne.path[x].coordinate[1] : parseFloat(distributionDataOne.path[x].coordinate[1])
                            if (distributionDataOne.path[x].coordinate[0] > distributionDataOne.path[x].coordinate[1]) {
                                marker = new BMap.Marker(new BMap.Point(distributionDataOne.path[x].coordinate[0], distributionDataOne.path[x].coordinate[1]), {
                                    icon: icon
                                });
                            } else {
                                marker = new BMap.Marker(new BMap.Point(distributionDataOne.path[x].coordinate[1], distributionDataOne.path[x].coordinate[0]), {
                                    icon: icon
                                });
                            }
                            content = `<p slot="title">
                                ${ distributionDataOne.path[x].addressName }
                            </p>`;
                            this.map.addOverlay(marker);
                            this.addClickHandler(content, marker);
                        }
                        continue;
                    }
                    distributionDataOne.path.coordinate[0] = parseFloat(distributionDataOne.path.coordinate[0]) ?
                        parseFloat(distributionDataOne.path.coordinate[0]) : distributionDataOne.path.coordinate[0]
                    distributionDataOne.path.coordinate[1] = parseFloat(distributionDataOne.path.coordinate[1]) ?
                        parseFloat(distributionDataOne.path.coordinate[1]) : distributionDataOne.path.coordinate[1]
                    distributionDataOne.path.coordinate[0] > distributionDataOne.path.coordinate[1] ?
                        (marker = new BMap.Marker(new BMap.Point(distributionDataOne.path.coordinate[0], distributionDataOne.path.coordinate[1]), {
                            icon: icon
                        })) :
                        (marker = new BMap.Marker(new BMap.Point(distributionDataOne.path.coordinate[1], distributionDataOne.path.coordinate[0]), {
                            icon: icon
                        }));
                    content = `
                    <p slot="title">
                        ${ distributionDataOne.path.addressName }
                    </p>`;
                    this.map.addOverlay(marker);
                    this.addClickHandler(content, marker);
                }
                this.map.removeOverlay(this.map.getOverlays()[2])
            },
            addClickHandler: function (content, marker) {
                let _this = this;
                marker.addEventListener("click", function (e) {
                    _this.openInfo(content, e)
                });
            },
            openInfo: function (content, e) {
                let opts = {
                    width: 250, // 信息窗口宽度
                    height: 80, // 信息窗口高度
                    // title: "信息窗口", // 信息窗口标题
                    enableMessage: true //设置允许信息窗发送短息
                };
                let p = e.target;
                let point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
                let infoWindow = new BMap.InfoWindow(content, opts);
                this.map.openInfoWindow(infoWindow, point);
            },
            getPathJsonNew: function (e,type) {
                let typ = "";
                if (!type) {
                    console.log("new")
                   typ = "new";
                }
                else{
                    console.log("old")
                    typ = "old";
                }
                let _this = this;
                let setting = {
                    method: 'post',
                    url: "../static/json/"+ typ + "-" + e +".json"
                }
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        if (this.model1) {
                            _this.comp_dis = res.data[0].Daydistance;
                            _this.comp_turn = res.data[0].pathInfo.length;
                            debugger;
                        }
                        _this.pathNewData = res.data[0].pathInfo;
                        _this.newDistance = res.data[0].Daydistance;
                        _this.newtrun = res.data[0].pathInfo.length;
                        this.initDrv(_this.pathNewData, '#1453e2', -1, 0);
                        this.addDistribution(_this.pathNewData);
                    } else {
                        console.log('ya,出錯了')
                    }
                }.bind(this), {
                    setting: setting
                })
            },
            getPathJsonOld: function (e) {
                let _this = this;
                let setting = {
                    method: 'post',
                    url: "../static/json/" + "old" + "-" + e +".json"
                }
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        _this.pathOldData = res.data[0].pathInfo;
                        _this.oldDistance = res.data[0].Daydistance;
                        _this.oldtrun = res.data[0].pathInfo.length;
                        this.initDrv(_this.pathOldData, '#008573', -1, 1);
                        this.addDistribution(_this.pathOldData);
                    } else {
                        console.log('ya,出錯了')
                    }
                }.bind(this), {
                    setting: setting
                })
            },
            getPathJsonComp: function (e) {
                let _this = this;
                let setting = {
                    method: 'post',
                    url: "../static/json/" + "old" + "-" + e +".json"
                }
                axios.call(this, function (res) {
                    if (res.code === 0) {
                        _this.comp_dis = res.data[0].Daydistance;
                        _this.oldtrun = res.data[0].pathInfo.length;
                    } else {
                        console.log('ya,出錯了')
                    }
                }.bind(this), {
                    setting: setting
                })
            },
            getData: function(e){
                this.map.clearOverlays();
                this.addFactoryPoint(); 
                this.getPathJsonOld(e);
                this.getPathJsonNew(e);
            },
            getSelectPath: function (e) {
                this.initDrv([], '#008573', e.id+1, e.dType)  //f10707
            },
            init_other_Chart(target){
                console.log("target:"+target)
            },
            init_mile_com(totalChart2,end_time){
                totalChart2.clear()
                let all_dis = this.oldDistance
                let new_dis = this.newDistance
                let option2 = {
                    title: [{
                        text: "\t\t\t\t\t\t\t\t\t\t\t\t里程对比\n"+"\t\t\t原始里程："+all_dis+"\n"+"节约后里程："+new_dis,
                        x: '26%',
                        y: '5%',
                        textStyle: {
                            fontSize: "17"
                        }
                    }, ],
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    series: [{
                        name: '路径里程优化',
                        type: 'pie',
                        radius: '50%',
                        // center: ['25%', '25%'],
                        clockwise: false,
                        data: [{
                            value: this.newDistance/1000,
                            name: '优化后里程/公里'
                        }, {
                            value: (this.oldDistance - this.newDistance)/1000,
                            name: '节约里程/公里'
                        }, ],
                        label: {
                            normal: {
                                textStyle: {
                                    color: '#999',
                                    fontSize: 12,
                                }
                            }
                        },
                    }],
                
                };
                if(end_time){
                    this.comp_chart(option2,end_time);
                }
                totalChart2.setOption(option2);
                this.chart_init(totalChart2,end_time)
            },
            init_turns_com(totalChart2,end_time){
                let trun_chart = echarts.init(document.getElementById('totalChart2'));
                trun_chart.clear()
                let old_t = this.oldtrun
                let new_t = this.newtrun
                let option3 = {
                    title: [{
                        text: "\t\t\t\t\t\t\t\t\t\t\t\t运单数对比\n"+"\t\t\t原始运单数："+old_t+"\n"+"节约后运单数："+new_t,
                        x: '26%',
                        y: '5%',
                        textStyle: {
                            fontSize: "17"
                        }
                    }, ],
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: ['原始运单数','节约后运单数']
                    },
                    series : [
                        {
                            name: '访问来源',
                            type: 'pie',
                            radius : '55%',
                            center: ['50%', '60%'],
                            data:[
                                {value:old_t, name:'原始运单数'},
                                {value:new_t, name:'节约后运单数'}
                            ],
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ],
                };
                if(end_time){
                    this.comp_chart2(option3,end_time);
                }
                trun_chart.setOption(option3);
                this.chart_init(trun_chart,end_time)
            },
            init_Average(totalChart2,end_time){
                let Average_chart = echarts.init(document.getElementById('totalChart2'));
                Average_chart.clear()
                var setting = {
                    method: "POST",
                    url: "../../public/index.php/index/Service/get_kmeans_mouth"
                }
                var data = {
                    mouth: this.starttime.split("-")[1],
                    date: this.starttime,
                    comp_date: this.endtime
                }
                axios.call(this, function(res) {
                    console.log(res.data)
                    if (res['code'] === 0) { 
                        let option2 = {
                            title : {
                                text: '月均值对比',
                            },
                            tooltip : {
                                trigger: 'axis'
                            },
                            legend: {
                                data:['当天数值','月均值']
                            },
                            toolbox: {
                                show : true,
                                feature : {
                                    dataView : {show: true, readOnly: false},
                                    magicType : {show: true, type: ['line', 'bar']},
                                    restore : {show: true},
                                    saveAsImage : {show: true}
                                }
                            },
                            calculable : true,
                            xAxis : [
                                {
                                    type : 'category',
                                    data : ['订单数','运单号','配送点']
                                }
                            ],
                            yAxis : [
                                {
                                    type : 'value'
                                }
                            ],  
                            series : [
                                {
                                    name:'当天数值',
                                    type:'bar',
                                    data:[parseFloat(res.data[3]).toFixed(1), parseFloat(this.oldtrun).toFixed(1),parseFloat(res.data[4]).toFixed(1)],
                                    markPoint : {
                                    },
                                    label: {
                                        normal: {
                                            show:true,
                                            position: 'top',
                                            // formatter:function(params){
                                            //     if(params.dataIndex==0&&params.value>10000){
                                            //         params.value=params.value/100000;
                                            //         return params.value+'万'
                                            //     }
                                            // }
                                        }
                                    },
                                },
                                {
                                    name:'月均值',
                                    type:'bar',
                                    data:[parseFloat(res.data[0]).toFixed(1),parseFloat(res.data[1]).toFixed(1),parseFloat(res.data[2]).toFixed(1)],
                                    markPoint : {
                                        data : [
                                            {name : '年最高', value : 182.2, xAxis: 7, yAxis: 183},
                                            {name : '年最低', value : 2.3, xAxis: 11, yAxis: 3}
                                        ]
                                    },
                                    label: {
                                        normal: {
                                            show:true,
                                            position: 'top',
                                            // formatter:function(params){
                                            //     if(params.dataIndex==0&&params.value>10000){
                                            //         params.value=params.value/100000;
                                            //         return params.value+'万'
                                            //     }
                                            // }
                                        }
                                    },
                                }
                            ]
                            
                    };
                    if(end_time){
                        // console.log(1)
                        this.comp_chart3(option2,end_time,res)
                    }
                    totalChart2.setOption(option2);
                    this.chart_init(totalChart2,end_time)
                    } 
                }.bind(this), {
                    setting,
                    data
                })
            },
            init_all_com(totalChart2,end_time){
                totalChart2.clear()
                let all_dis = this.oldDistance
                let new_dis = this.newDistance
                let option2 = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'left',
                        data:['抽取订单样本数','订单样本总数']
                    },
                    series: [
                        {
                            name:'抽取订单样本数',
                            type:'pie',
                            selectedMode: 'single',
                            radius: [1, '35%'],
                
                            label: {
                                normal: {
                                    position: 'inner'
                                }
                            },
                            labelLine: {
                                normal: {
                                    show: false
                                }
                            },
                            data:[
                                {value:new_dis, name:'算法计算里程'},
                                {value:all_dis, name:'原始距离里程'},
                            ]
                        },
                        {
                            name:'订单样本总数',

                            type:'pie',
                            radius: ['40%', '55%'],
                            label: {
                                normal: {
                                    formatter: '{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}  {per|{d}%}  ',
                                    backgroundColor: '#eee',
                                    borderColor: '#aaa',
                                    borderWidth: 1,
                                    borderRadius: 4,
                                    // shadowBlur:3,
                                    // shadowOffsetX: 2,
                                    // shadowOffsetY: 2,
                                    // shadowColor: '#999',
                                    // padding: [0, 7],
                                    rich: {
                                        a: {
                                            color: '#999',
                                            lineHeight: 15,
                                            align: 'center'
                                        },
                                        // abg: {
                                        //     backgroundColor: '#333',
                                        //     width: '100%',
                                        //     align: 'right',
                                        //     height: 22,
                                        //     borderRadius: [4, 4, 0, 0]
                                        // },
                                        hr: {
                                            borderColor: '#aaa',
                                            width: '100%',
                                            borderWidth: 0.5,
                                            height: 0
                                        },
                                        b: {
                                            fontSize: 12,
                                            lineHeight: 22
                                        },
                                        per: {
                                            color: '#eee',
                                            backgroundColor: '#334455',
                                            padding: [2, 4],
                                            borderRadius: 2
                                        }
                                    }
                                }
                            },
                            data:[
                                {value:25000},
                                {value:297897},
                            ]
                        }
                    ]
            };
            totalChart2.setOption(option2);
            this.chart_init(totalChart2,end_time,res)
            },
            comp_chart(option2,end_time){
                    let comp_one = end_time
                    let comp_two = this.starttime
                    option2.title[0].text = "\t\t\t\t\t\t\t\t\t\t\t\t里程对比\n"+"\t\t\t"+comp_one+"："+this.oldDistance/1000+"\n"+comp_two+"："+this.comp_dis/1000
                    option2.series[0].data[0].value = this.oldDistance/1000
                    option2.series[0].data[0].name = comp_one+"/公里"
                    option2.series[0].data[1].value = this.comp_dis/1000
                    option2.series[0].data[1].name = comp_two+"/公里"
            },
            comp_chart2(option2,end_time){
                let comp_one = end_time
                let comp_two = this.starttime
                option2.title[0].text = "\t\t\t\t\t\t\t\t\t\t\t\t运送车次对比\n"+"\t\t\t"+comp_one+"："+this.oldtrun+"\n"+comp_two+"："+this.comp_turn
                option2.series[0].data[0].value = this.oldtrun
                option2.series[0].data[0].name = comp_one+"/车次"
                option2.series[0].data[1].value = this.comp_turn
                option2.series[0].data[1].name = comp_two+"/车次"
            },
            comp_chart3(option2,end_time,res){
                option2.title.text = "数据对比"
                option2.legend.data[0] = end_time
                option2.legend.data[1] = this.starttime
                option2.xAxis[0].data = ['运送总距离','运送车次','配送点']
                option2.series[0].name = this.starttime
                option2.series[1].name = end_time   
                option2.series[0].data = [this.oldDistance, this.oldtrun, res.data[4]]
                option2.series[1].data = [this.comp_dis, this.comp_turn, res.data[6]]
                if(this.oldDistance>10000){
                    
                    option2.series[0].label.normal.formatter = (params1)=>{
                            if (params1.dataIndex==0) {
                            return params1.value+'万'
                        }  
                    }
                    option2.series[0].data[0] = this.oldDistance/1000
                }
                if(this.comp_dis>1000){
                    
                        option2.series[1].label.normal.formatter = (params2)=>{
                                if (params2.dataIndex==0) {
                                return params2.value+"万"
                            }  
                    }
                    
                    option2.series[1].data[0] =this.comp_dis/10000
                }
                // if(this.comp_dis>10000){
                //     this.comp_dis=this.comp_dis/10000
                // }
                // if(this.oldDistance>10000){
                //     this.oldDistance=this.oldDistance/10000
                // }
                
            },
            chart_init(totalChart2,end_time){
                document.getElementById('mile_com').onclick =()=>{
                    this.init_mile_com(totalChart2,end_time);
                }
                document.getElementById('turns_com').onclick =()=>{
                    this.init_turns_com(totalChart2,end_time);
                }
                document.getElementById('Average').onclick =()=>{
                    this.init_Average(totalChart2,end_time);
                }
                document.getElementById('all_com').onclick =()=>{
                    this.init_all_com(totalChart2,end_time);
                }
            },
            drawChart() {
                let end_time = this.endtime
                let all_dis = this.oldDistance
                let new_dis = this.newDistance
                let totalChart2 = echarts.init(document.getElementById('totalChart2'));
                let option2 = {
                    title: [{
                        text: "\t\t\t\t\t\t\t\t\t\t\t\t里程对比\n"+"\t\t\t原始里程："+all_dis/1000+"\n"+"节约后里程："+(this.oldDistance - this.newDistance)/1000,
                        x: '26%',
                        y: '5%',
                        textStyle: {
                            fontSize: "17"
                        }
                    }, ],
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    series: [{
                        name: '里程对比',
                        type: 'pie',
                        radius: '50%',
                        // center: ['25%', '25%'],
                        clockwise: false,
                        data: [{
                            value: this.newDistance/1000,
                            name: '优化后里程/公里'
                        }, {
                            value: (this.oldDistance - this.newDistance)/1000,
                            name: '节约里程/公里'
                        }, ],
                        label: {
                            normal: {
                                textStyle: {
                                    color: '#999',
                                    fontSize: 12,
                                }
                            }
                        },
                    }],
                };
                if(end_time){
                    this.comp_chart(option2,end_time);
                }
                totalChart2.clear();
                totalChart2.setOption(option2);
                this.chart_init(totalChart2,end_time);
            }
        },
        created() {}
    });
});